package xhwl.hmo.mall.ui;

import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lidroid.xutils.exception.DbException;
import com.trello.rxlifecycle.components.RxActivity;

import java.util.ArrayList;
import java.util.List;

import xhwl.hmo.mall.BaseActivity;
import xhwl.hmo.mall.R;
import xhwl.hmo.mall.adapter.SearchAdapter;
import xhwl.hmo.mall.adapter.TagAdapter;
import xhwl.hmo.mall.db.HistoryDao;
import xhwl.hmo.mall.db.bean.History;
import xhwl.hmo.mall.http.api.CardStatusApi;
import xhwl.hmo.mall.http.api.HotSearchApi;
import xhwl.hmo.mall.http.api.SearchRecommendApi;
import xhwl.hmo.mall.http.bean.resp.CardStatusResp;
import xhwl.hmo.mall.http.bean.resp.HttpResp;
import xhwl.hmo.mall.views.ClearEditText;
import xhwl.hmo.mall.views.FlowLayout;
import xhwl.hmo.mall.views.TagFlowLayout;
import xhwl.retrofitrx.HttpManager;
import xhwl.retrofitrx.OnNextListener;

public class SearchActivity extends BaseActivity implements View.OnClickListener {
    private ClearEditText et_clear;
    private TagFlowLayout mFlowLayout, mflowlayoutsearch;
    HistoryDao historyDao;
    TextView tv_cancel;
    GridView gv_search;
    private List<History> strings = new ArrayList<>();
    private List<History> historyList = new ArrayList<>();
    private List<CardStatusResp> hotsearch = new ArrayList();
    //布局管理器
    private LayoutInflater mInflater;
    private TextView tv;
    private LinearLayout lin_history, lin_nogood,lin_nogoodall;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);

    }

    @Override
    public void initViews() {
        lin_nogoodall = (LinearLayout) findViewById(R.id.lin_nogoodall);
        lin_nogood = (LinearLayout) findViewById(R.id.lin_nogood);
        lin_history = (LinearLayout) findViewById(R.id.lin_history);
        gv_search = (GridView) findViewById(R.id.gv_search);
        mFlowLayout = (TagFlowLayout) findViewById(R.id.id_flowlayout);
        mflowlayoutsearch = (TagFlowLayout) findViewById(R.id.id_flowlayoutsearch);
        tv_cancel = (TextView) findViewById(R.id.tv_cancel);
        et_clear = (ClearEditText) findViewById(R.id.et_clear);
        historyDao = new HistoryDao(SearchActivity.this);
        try {
            strings = historyDao.findAll();
        } catch (DbException e) {
            e.printStackTrace();
        }
        for (int i = strings.size() - 1; i >= 0; i--) {
            historyList.add(strings.get(i));
        }

        et_clear.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    search();
                    return true;
                }
                return false;
            }
        });
        mInflater = LayoutInflater.from(this);
        mFlowLayout.setAdapter(new TagAdapter<History>(historyList) {
            @Override
            public View getView(FlowLayout parent, int position, History s) {
                tv = (TextView) mInflater.inflate(R.layout.tv,
                        mFlowLayout, false);
                tv.setText(s.history_name);
                return tv;
            }
        });
    }


    @Override
    public void initListener() {

        tv_cancel.setOnClickListener(this);
        mFlowLayout.setOnTagClickListener(new TagFlowLayout.OnTagClickListener() {
            @Override
            public boolean onTagClick(View view, int position, FlowLayout parent) {
                et_clear.setText(historyList.get(position).history_name);
                et_clear.setSelection(historyList.get(position).history_name.length());
                search();
//                Intent intent=new Intent(SearchActivity.this,SuiteProductsDetailActivity.class);
//                startActivity(intent);
//                Toast.makeText(SearchActivity.this, historyList.get(position).history_name, Toast.LENGTH_SHORT).show();
                return true;
            }
        });
        mflowlayoutsearch.setOnTagClickListener(new TagFlowLayout.OnTagClickListener() {
            @Override
            public boolean onTagClick(View view, int position, FlowLayout parent) {
                et_clear.setText(hotsearch.get(position).GOODS_NAME);
                et_clear.setSelection(hotsearch.get(position).GOODS_NAME.length());
                search();
// Intent intent=new Intent(SearchActivity.this,SuiteProductsDetailActivity.class);
//                startActivity(intent);
//                Toast.makeText(SearchActivity.this, historyList.get(position).history_name, Toast.LENGTH_SHORT).show();
                return true;
            }
        });
        gv_search.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            }
        });
    }

    @Override
    public void loadData() {
        HotSearchApi hotSearchApi = new HotSearchApi();
        HttpManager.getInstance().dealHttp((RxActivity) mContext, hotSearchApi, new OnNextListener<HttpResp<ArrayList<CardStatusResp>>>() {
            @Override
            public void onNext(HttpResp<ArrayList<CardStatusResp>> cardstatusResp) {

                hotsearch.addAll(cardstatusResp.data);
                mflowlayoutsearch.setAdapter(new TagAdapter<CardStatusResp>(hotsearch) {
                    @Override
                    public View getView(FlowLayout parent, int position, CardStatusResp s) {
                        tv = (TextView) mInflater.inflate(R.layout.tv,
                                mFlowLayout, false);
                        tv.setText(s.GOODS_NAME);
                        return tv;
                    }
                });

            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_cancel:
                finish();
                break;
        }
    }

    public void search() {
        CardStatusApi cardStatusApi = new CardStatusApi("", et_clear.getText().toString().trim(), 0);

        History history = new History();
        history.history_name = et_clear.getText().toString().trim();

        try {
            for(int i=0;i<strings.size();i++){
                if (strings.get(i).history_name.equals(et_clear.getText().toString().trim())){
                    historyDao.delete(history);
                }
            }
            historyDao.save(history);
        } catch (DbException e) {
            e.printStackTrace();
        }

        HttpManager.getInstance().dealHttp(SearchActivity.this, cardStatusApi, new OnNextListener<HttpResp<ArrayList<CardStatusResp>>>() {
            @Override
            public void onNext(HttpResp<ArrayList<CardStatusResp>> arrayListHttpResp) {
                if (arrayListHttpResp.isOk()) {
                    if (arrayListHttpResp.data.size() == 0) {
                        lin_nogoodall.setVisibility(View.VISIBLE);
                        lin_history.setVisibility(View.GONE);
                        lin_nogood.setVisibility(View.VISIBLE);
                        gv_search.setVisibility(View.VISIBLE);
                        SearchRecommendApi searchRecommendApi = new SearchRecommendApi();
                        HttpManager.getInstance().dealHttp(SearchActivity.this, searchRecommendApi, new OnNextListener<HttpResp<ArrayList<CardStatusResp>>>() {
                            @Override
                            public void onNext(HttpResp<ArrayList<CardStatusResp>> arrayList) {
                                if (arrayList.isOk()) {
                                    SearchAdapter searchAdapter = new SearchAdapter(mContext, arrayList.data);
                                    gv_search.setAdapter(searchAdapter);
                                }
                            }
                        });
                    } else {
                        lin_nogoodall.setVisibility(View.VISIBLE);
                        lin_nogood.setVisibility(View.GONE);
                        lin_history.setVisibility(View.GONE);
                        gv_search.setVisibility(View.VISIBLE);
                        SearchAdapter searchAdapter = new SearchAdapter(mContext, arrayListHttpResp.data);
                        gv_search.setAdapter(searchAdapter);
                    }

                }
            }
        });


    }

}

